github.com/pbberlin/tools@v0.0.0-20160910141205-7aa5421c2169/dsu/ancestored_urls/insert and query with or without ancestor.go (about) 1 package ancestored_urls 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "net/http" 8 9 "github.com/pbberlin/tools/net/http/loghttp" 10 "github.com/pbberlin/tools/util" 11 12 "appengine" 13 ds "appengine/datastore" 14 ) 15 16 type LastURL struct { 17 Value string 18 } 19 20 const keyNoAncInt = 0 21 const kindUrl = "classUrl" 22 23 // 5623589659213824 24 const keyUrl = "constant__KeyUrl" 25 26 const kindUrlParent = "classUrlParent" 27 const keyUrlParent = "keyUrlParent" 28 29 func ancKey(c appengine.Context) *ds.Key { 30 return ds.NewKey(c, kindUrlParent, keyUrlParent, 0, nil) 31 } 32 33 // saving some data by kind and key 34 // without ancestor key 35 func saveURLNoAnc(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { 36 37 c := appengine.NewContext(r) 38 39 k := ds.NewKey(c, kindUrl, keyUrl, 0, nil) 40 e := new(LastURL) 41 err := ds.Get(c, k, e) 42 if err == ds.ErrNoSuchEntity { 43 c.Errorf("%v", err) 44 } else { 45 loghttp.E(w, r, err, false) 46 } 47 48 old := e.Value 49 e.Value = r.URL.Path + r.URL.RawQuery 50 51 _, err = ds.Put(c, k, e) 52 loghttp.E(w, r, err, false) 53 54 w.Header().Set("Content-Type", "text/plain; charset=utf-8") 55 w.Write([]byte("old=" + old + "\n")) 56 w.Write([]byte("new=" + e.Value + "\n")) 57 58 } 59 60 func saveURLWithAncestor(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { 61 62 c := appengine.NewContext(r) 63 64 k := ds.NewKey(c, kindUrl, "", 0, ancKey(c)) 65 66 s := util.TimeMarker() 67 ls := len(s) 68 lc := len("09-26 17:29:25") 69 lastURL_fictitious_1 := LastURL{"with_anc " + s[ls-lc:ls-3]} 70 _, err := ds.Put(c, k, &lastURL_fictitious_1) 71 loghttp.E(w, r, err, false) 72 73 } 74 75 // get all URLs 76 // not just by ancestor 77 // results might be delayed 78 func listURLNoAnc(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { 79 80 w.Header().Set("Content-Type", "text/plain; charset=utf-8") 81 82 c := appengine.NewContext(r) 83 84 b1 := new(bytes.Buffer) 85 q := ds.NewQuery(kindUrl). 86 Filter("Value>", `/save`). 87 Order("-Value") 88 89 i := -1 90 for t := q.Run(c); ; { 91 i++ 92 var lu LastURL 93 key, err := t.Next(&lu) 94 if err == ds.Done { 95 b1.WriteString("\nds.Done\n") 96 break 97 } 98 if err != nil { 99 http.Error(w, err.Error(), http.StatusInternalServerError) 100 return 101 } 102 fmt.Fprint(b1, "q loop ", i, "\n") 103 fmt.Fprintf(b1, "\tKey %64s \n\tVal %64s\n", key, lu.Value) 104 } 105 106 w.Write(b1.Bytes()) 107 108 } 109 110 // get all ancestor urls 111 // ordering possible 112 // distinction to above: *always* consistent 113 func listURLWithAncestors(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { 114 115 w.Header().Set("Content-Type", "text/plain; charset=utf-8") 116 117 c := appengine.NewContext(r) 118 119 q := ds.NewQuery(kindUrl). 120 Ancestor(ancKey(c)). 121 Order("-Value") 122 var vURLs []LastURL 123 keys, err := q.GetAll(c, &vURLs) 124 loghttp.E(w, r, err, false) 125 126 for i, v := range vURLs { 127 io.WriteString(w, fmt.Sprint("q loop ", i, "\n")) 128 io.WriteString(w, fmt.Sprintf("\tKey %64s \n\tVal %64s\n", keys[i], v.Value)) 129 } 130 io.WriteString(w, "\nds.Done\n") 131 132 }